package org.dcarew.notification.handlers;

import org.dcarew.notification.layout.NotificationLayout;
import org.dcarew.notification.ui.NotificationControl;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.internal.EditorAreaHelper;
import org.eclipse.ui.internal.EditorSashContainer;
import org.eclipse.ui.internal.LayoutTree;
import org.eclipse.ui.internal.WorkbenchPage;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class SampleHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public SampleHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		
		IWorkbenchPage page = window.getActivePage();
		
    //IEditorPart editorPart = page.getActiveEditor();
    
    //IEditorSite editorSite = editorPart.getEditorSite();
    
    WorkbenchPage wPage = (WorkbenchPage)page;
    
    // editorPresentation
    EditorAreaHelper editorAreaHelper = wPage.getEditorPresentation();
    
    EditorSashContainer editorSashContainer = (EditorSashContainer)editorAreaHelper.getLayoutPart();
    
    LayoutTree layoutTree = editorSashContainer.getLayoutTree();
    
    Composite parent = editorSashContainer.getParent();
    
    //parent.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
    
    if (parent.getLayout() == null) {
      NotificationControl notificationControl = new NotificationControl(parent, SWT.NONE);
      
//      Label label = new Label(parent, SWT.NONE);
//      label.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_CYAN));
//      label.setText("TODO alert test control");
      
      NotificationLayout layout = new NotificationLayout();
      layout.setAlertControl(notificationControl);
      parent.setLayout(layout);
      
      //GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent);
      
      parent.layout();
    } else {
      NotificationLayout layout = (NotificationLayout)parent.getLayout();
      
      layout.getAlertControl().dispose();
      layout.setAlertControl(null);
      
      parent.layout();
      
      parent.setLayout(null);
    }
    
		return null;
	}
}
